﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ESRI.ArcGIS.Carto;
using ESRI.ArcGIS.Geodatabase;

namespace Mymap
{
    public partial class FeatureAttributeTable : UserControl
    {
        private IFeatureLayer m_featureLayer;

        public FeatureAttributeTable(IFeatureLayer featureLayer)
        {
            // 存储传入的要素图层到私有变量（与FieldSelectDialog保持一致）
            this.m_featureLayer = featureLayer;

            InitializeComponent(); // 初始化控件（必须调用，设计器生成的方法）

            // 绑定Load事件到tableLoaded方法（确保事件响应函数能被触发）
            this.Load += new EventHandler(FeatureAttributeTable_Load);
        }


       
        private void FeatureAttributeTable_Load(object sender, EventArgs e)
        {
            IFeatureClass featureClass = m_featureLayer.FeatureClass;

            DataTable table = new DataTable();
            IFields fields = featureClass.Fields;
            for (int i = 0; i < fields.FieldCount; i++)
            {
                IField field = fields.get_Field(i);
                DataColumn column = new DataColumn(field.Name);
                column.DataType = System.Type.GetType("System.String");
                table.Columns.Add(column);
            }

            IFeatureCursor featureCursor = featureClass.Search(null, false);
            IFeature feature = featureCursor.NextFeature();
            while (feature != null)
            {
                DataRow row = table.NewRow();
                for (int i = 0; i < fields.FieldCount; i++)
                {
                    row[i] = feature.get_Value(i).ToString();
                }
                table.Rows.Add(row);
                feature = featureCursor.NextFeature();
            }

            dataGridView1.DataSource = table;
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}
